﻿@model RepertorySystem.Models.InputStorage
@Scripts.Render("~/bundles/jquery")
@{
    ViewBag.Title = "Create";
}

<h2>创建入库单 <span class="glyphicon glyphicon-paperclip" style="color: rgb(69, 63, 63);"></span></h2>


@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>订单信息</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Supplier, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Supplier, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Supplier, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Book_Id, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @{
                    //获取端口
                    IServiceProvider provider = (IServiceProvider)HttpContext.Current;
                    HttpWorkerRequest request = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
                    var port = HttpContext.Current.Request.Url.Port;

                }
                <script type="text/javascript">
                    $(function () {
                        $.ajax({
                            url:  "http://localhost:"+@port+"/api/Books",
                            type: "get",
                            success: function (result) {
                                $.each(result, function (index, obj) {
                                    $("#book_select").append("<option value="+obj['ID']+">"+obj['Name']+"</option>");
                                });
                               
                            },
                            error: function (msg) {
                                console.log(msg);
                            }

                        });
                        
                    });
                </script>
              
                <select class="form-control" data-val="true" data-val-number="字段 必须是一个数字。" id="book_select" name="Book_Id" style="width:180px">
                    <option value="">请选择书籍</option>
                </select>
               
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="Number" class="form-control" id="book_Number1"/>
                @Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Time, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Time, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Time, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="Price" class="form-control" id="book_Price"/>

                <script type="text/javascript">
                    $("#book_Price").focus(function () {
                        var book_Id = $("#book_select option:selected").val();
                        var book_Number = $("#book_Number1").val();
                        $.ajax({
                            url:  "http://localhost:"+@port+"/api/Books/"+book_Id,
                            type: "get",
                            success: function (result) {
                                var sumPrice = result.Price * book_Number
                                $("#book_Price").val(sumPrice);
                            },
                            error: function (msg) {
                                console.log(msg);
                            }
                        });
                    })
                </script>

            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-1">
                <input id="sub" type="submit"  value="创建订单" class="btn btn-default" />
                <script type="text/javascript">
                    $("#sub").click(function () {
                        @*var book_Id = $("#book_select option:selected").val();
                        $.ajax({
                            url: "http://localhost:" +@port +"/api/Storages/" + book_Id,
                            type: "get",
                            success: function (result) {
                                if (result != null) {

                                }
                                
                            },
                            error: function (msg) {
                                console.log(msg);
                            }
                        });*@
                        alert("入库成功！，请转至库存管理查看");
                    });
                </script>
            </div>
            <div class="col-md-2">
                @Html.ActionLink("返回订单列表", "Index", null, new { @class = "btn btn-default" })
            </div>
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
